package pl.ptemich.tankwars.states;



import org.jbox2d.collision.shapes.MassData;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import pl.ptemich.tankwars.tools.StateTools;

public class JBoxTestState extends RegisteredState {
  
  private float targetFPS = 75;
  private float timeStep = (1 / targetFPS);
  private int velocityIterations = 8;
  private int positionIterations = 3;


  //private AABB worldAABB;
  private World world;
  private Body groundBody;  
  private Body simulatedBody;
  private Body simulatedBody2;
  
  
  @Override
  public void init(GameContainer arg0, StateBasedGame arg1)
      throws SlickException {
    // Swiat z grawitacja
    Vec2 gravity = new Vec2(0f, -10f);
    boolean doSleep = true;
    world = new World(gravity, doSleep);

    // Podloze
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyType.STATIC;
    groundBodyDef.position.set(new Vec2(0f, -10f));
    groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(50f, 10f); // połowka wymiaru jako parametr
    groundBody.createFixture(groundBox, 1f);
    
    MassData massData = new MassData();
    
    // pierwszy obiekt 
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(20f, 80f);
    simulatedBody = world.createBody(bodyDef);
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(1f, 1f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 10f; // tarcie
    fixtureDef.restitution = 0.5f;
    simulatedBody.createFixture(fixtureDef);
    simulatedBody.getMassData(massData);
    massData.mass = 100f;
    simulatedBody.setMassData(massData);
    //simulatedBody.resetMassData();
    
    bodyDef.position.set(40f, 80f);
    simulatedBody2 = world.createBody(bodyDef);;
    fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 2000.0f;
    fixtureDef.friction = 0f; // tarcie
    fixtureDef.restitution = 0.5f;
    simulatedBody2.createFixture(fixtureDef);
    
    massData = new MassData();
    simulatedBody2.getMassData(massData);
    massData.mass = 1000f;
    simulatedBody2.setMassData(massData);
    //simulatedBody2.resetMassData();
  }
  
  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics graphics)
      throws SlickException {
    graphics.setColor(Color.blue);
    graphics.fillRect(simulatedBody.getPosition().x, 300-simulatedBody.getPosition().y, 10, 10);
    graphics.setColor(Color.red);
    graphics.fillRect(simulatedBody2.getPosition().x, 300-simulatedBody2.getPosition().y, 10, 10);
    graphics.setColor(Color.white);
    graphics.fillRect(groundBody.getPosition().x, 300-groundBody.getPosition().y, 200, 10);
    
  }
  
  @Override
  public void update(GameContainer container, StateBasedGame game, int delta)
      throws SlickException {
    Input input = container.getInput();
    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      StateTools.stateTransition(MenuState.class);
    }
    
    timeStep = (delta/1000f);
    
    world.step(timeStep, velocityIterations, positionIterations);

    //LOGGER.debug(timeStep + ":" + simulatedBody.getPosition().x + " - " + simulatedBody.getPosition().y + " " + simulatedBody.getAngle());       
  }
  
}
